package mock_seed

import (
	"fmt"
	"wms/internal/model"
	"wms/pkg/database"
	"wms/pkg/logger"

	"errors"

	"go.uber.org/zap"
	"gorm.io/gorm"
)

// InitFrozenFoodCargos 初始化餐饮冻品货物数据
func InitFrozenFoodCargos() error {
	logger.Info("开始初始化餐饮冻品货物数据...")
	db := database.GetDB()

	// 检查 cargos 表是否已有数据
	var existingCargo model.Cargo
	err := db.Select("id").First(&existingCargo).Error
	if err == nil {
		logger.Info("货物数据已存在，跳过初始化。")
		return nil
	} else if !errors.Is(err, gorm.ErrRecordNotFound) {
		logger.Error("检查货物数据失败", zap.Error(err))
		return err
	}
	logger.Info("货物数据表为空，开始填充...")

	// 辅助函数创建uint指针
	uintPtr := func(i uint) *uint {
		return &i
	}

	// 当前用户ID (假设为系统管理员，ID为1)
	const adminID uint = 1

	// 获取所有分类
	var categories []model.CargoCategory
	if err := db.Find(&categories).Error; err != nil {
		logger.Error("获取分类失败", zap.Error(err))
		return err
	}

	// 获取所有供应商
	var suppliers []model.Supplier
	if err := db.Find(&suppliers).Error; err != nil {
		logger.Error("获取供应商失败", zap.Error(err))
		return err
	}

	// 打印供应商信息
	for _, supplier := range suppliers {
		logger.Info(fmt.Sprintf("找到供应商: ID=%d, 名称=%s", supplier.ID, supplier.Name))
	}

	// 创建分类名称到ID的映射
	categoryMap := make(map[string]uint)
	for _, category := range categories {
		categoryMap[category.Name] = category.ID
		logger.Info(fmt.Sprintf("找到分类: ID=%d, 名称=%s", category.ID, category.Name))
	}

	// 根据用户提供的结构创建货物
	cargos := []model.Cargo{
		// 鸡肉类产品
		{
			Code:         "CHICKEN-001",
			Name:         "冻鸡翅中",
			CategoryID:   categoryMap["鸡肉"],
			SupplierID:   uintPtr(4), // 金龙速冻食品有限公司
			Unit:         "箱",
			Spec:         "10kg/箱",
			Price:        198.0,
			Description:  "优质鸡翅中段，每箱约500g×20包",
			MinStock:     10,
			MaxStock:     100,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    300,
			Manufacturer: "正大食品有限公司",
			Remark:       "适合炸鸡翅、烤翅等，肉质紧实有弹性",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},
		{
			Code:         "CHICKEN-002",
			Name:         "冻鸡腿",
			CategoryID:   categoryMap["鸡肉"],
			SupplierID:   uintPtr(4), // 金龙速冻食品有限公司
			Unit:         "箱",
			Spec:         "12kg/箱",
			Price:        216.0,
			Description:  "整只鸡腿，每箱约30只",
			MinStock:     8,
			MaxStock:     80,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    270,
			Manufacturer: "新希望集团",
			Remark:       "适合烤制、炖煮等多种烹饪方式",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},
		{
			Code:         "CHICKEN-003",
			Name:         "冻鸡胸肉",
			CategoryID:   categoryMap["鸡肉"],
			SupplierID:   uintPtr(4), // 金龙速冻食品有限公司
			Unit:         "箱",
			Spec:         "10kg/箱",
			Price:        185.0,
			Description:  "去骨鸡胸肉片，每箱10包×1kg",
			MinStock:     15,
			MaxStock:     120,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    240,
			Manufacturer: "泰森食品",
			Remark:       "低脂高蛋白，适合健身人士及各种快餐",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},

		// 鸭肉类产品
		{
			Code:         "DUCK-001",
			Name:         "冻白条鸭",
			CategoryID:   categoryMap["鸭肉"],
			SupplierID:   uintPtr(4), // 金龙速冻食品有限公司
			Unit:         "箱",
			Spec:         "15kg/箱",
			Price:        268.0,
			Description:  "整只白条鸭，每箱6只，约2.5kg/只",
			MinStock:     5,
			MaxStock:     40,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    210,
			Manufacturer: "金星鸭业有限公司",
			Remark:       "适合烤鸭、酱鸭等传统烹饪",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},

		// 猪肉-分割部位
		{
			Code:         "PORK-001",
			Name:         "冻猪肋排",
			CategoryID:   categoryMap["分割部位"],
			SupplierID:   uintPtr(1), // 北极冷鲜肉有限公司
			Unit:         "箱",
			Spec:         "10kg/箱",
			Price:        235.0,
			Description:  "精选猪肋排，每箱约20根",
			MinStock:     10,
			MaxStock:     60,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    180,
			Manufacturer: "双汇集团",
			Remark:       "适合红烧、糖醋等烹饪方式",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},
		{
			Code:         "PORK-002",
			Name:         "冻猪五花肉",
			CategoryID:   categoryMap["分割部位"],
			SupplierID:   uintPtr(1), // 北极冷鲜肉有限公司
			Unit:         "箱",
			Spec:         "12kg/箱",
			Price:        210.0,
			Description:  "优质猪五花肉，切块，每箱24包×500g",
			MinStock:     15,
			MaxStock:     90,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    180,
			Manufacturer: "雨润食品",
			Remark:       "适合红烧肉、回锅肉等经典菜式",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},

		// 猪肉-副产品
		{
			Code:         "PORK-003",
			Name:         "冻猪耳",
			CategoryID:   categoryMap["副产品"],
			SupplierID:   uintPtr(1), // 北极冷鲜肉有限公司
			Unit:         "箱",
			Spec:         "5kg/箱",
			Price:        168.0,
			Description:  "精选猪耳朵，每箱约50只",
			MinStock:     8,
			MaxStock:     50,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    150,
			Manufacturer: "金锣食品",
			Remark:       "适合凉拌、卤制等烹饪方式",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},

		// 牛肉类产品
		{
			Code:         "BEEF-001",
			Name:         "冻牛肉卷",
			CategoryID:   categoryMap["牛肉"],
			SupplierID:   uintPtr(1), // 北极冷鲜肉有限公司
			Unit:         "箱",
			Spec:         "8kg/箱",
			Price:        385.0,
			Description:  "精选牛肉卷，每箱16盒×500g",
			MinStock:     10,
			MaxStock:     60,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    270,
			Manufacturer: "恒都牛肉",
			Remark:       "适合火锅、铁板烧等烹饪方式",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},
		{
			Code:         "BEEF-002",
			Name:         "冻牛腩",
			CategoryID:   categoryMap["牛肉"],
			SupplierID:   uintPtr(1), // 北极冷鲜肉有限公司
			Unit:         "箱",
			Spec:         "10kg/箱",
			Price:        325.0,
			Description:  "澳洲进口牛腩，切块，每箱20包×500g",
			MinStock:     8,
			MaxStock:     45,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    300,
			Manufacturer: "顺鑫农业",
			Remark:       "适合炖煮，肉质紧实，纹理清晰",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},

		// 羊肉类产品
		{
			Code:         "LAMB-001",
			Name:         "冻羊排",
			CategoryID:   categoryMap["羊肉"],
			SupplierID:   uintPtr(3), // 海洋冷冻食品有限公司
			Unit:         "箱",
			Spec:         "6kg/箱",
			Price:        468.0,
			Description:  "新西兰进口羊排，每箱12块×500g",
			MinStock:     5,
			MaxStock:     30,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    240,
			Manufacturer: "恒都牛羊肉",
			Remark:       "适合烧烤、红烧等烹饪方式",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},

		// 鱼类产品
		{
			Code:         "FISH-001",
			Name:         "冻三文鱼片",
			CategoryID:   categoryMap["鱼类"],
			SupplierID:   uintPtr(3), // 海洋冷冻食品有限公司
			Unit:         "箱",
			Spec:         "5kg/箱",
			Price:        520.0,
			Description:  "挪威进口三文鱼片，每箱10包×500g",
			MinStock:     8,
			MaxStock:     40,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    180,
			Manufacturer: "挪威海产",
			Remark:       "适合刺身、烤制等多种烹饪方式",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},
		{
			Code:         "FISH-002",
			Name:         "冻带鱼段",
			CategoryID:   categoryMap["鱼类"],
			SupplierID:   uintPtr(3), // 海洋冷冻食品有限公司
			Unit:         "箱",
			Spec:         "10kg/箱",
			Price:        298.0,
			Description:  "东海带鱼段，每箱20包×500g",
			MinStock:     12,
			MaxStock:     80,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    150,
			Manufacturer: "舟山水产",
			Remark:       "适合红烧、清蒸等烹饪方式",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},

		// 虾蟹类产品
		{
			Code:         "SHRIMP-001",
			Name:         "冻南美白虾",
			CategoryID:   categoryMap["虾蟹类"],
			SupplierID:   uintPtr(3), // 海洋冷冻食品有限公司
			Unit:         "箱",
			Spec:         "10kg/箱",
			Price:        680.0,
			Description:  "南美进口白虾，每箱40-60只/kg",
			MinStock:     6,
			MaxStock:     30,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    270,
			Manufacturer: "南美水产",
			Remark:       "适合烧烤、清蒸等多种烹饪方式",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},
		{
			Code:         "CRAB-001",
			Name:         "冻梭子蟹",
			CategoryID:   categoryMap["虾蟹类"],
			SupplierID:   uintPtr(3), // 海洋冷冻食品有限公司
			Unit:         "箱",
			Spec:         "8kg/箱",
			Price:        760.0,
			Description:  "东海梭子蟹，每箱约16只",
			MinStock:     5,
			MaxStock:     20,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    150,
			Manufacturer: "舟山水产",
			Remark:       "适合蒸、煮等简单烹饪",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},

		// 腌制/半成品
		{
			Code:         "PREP-001",
			Name:         "腌制鸡翅",
			CategoryID:   categoryMap["腌制/半成品"],
			SupplierID:   uintPtr(5), // 天坊冷链物流有限公司
			Unit:         "箱",
			Spec:         "8kg/箱",
			Price:        256.0,
			Description:  "香辣口味腌制鸡翅，每箱16包×500g",
			MinStock:     15,
			MaxStock:     80,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    120,
			Manufacturer: "正大食品",
			Remark:       "预腌制，可直接烧烤或炸制",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},
		{
			Code:         "PREP-002",
			Name:         "火锅牛肉丸",
			CategoryID:   categoryMap["腌制/半成品"],
			SupplierID:   uintPtr(5), // 天坊冷链物流有限公司
			Unit:         "箱",
			Spec:         "6kg/箱",
			Price:        198.0,
			Description:  "手打牛肉丸，每箱12包×500g",
			MinStock:     15,
			MaxStock:     80,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    150,
			Manufacturer: "安井食品",
			Remark:       "适合火锅、关东煮等烹饪",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},

		// 成品菜肴
		{
			Code:         "MEAL-001",
			Name:         "宫保鸡丁",
			CategoryID:   categoryMap["成品菜肴"],
			SupplierID:   uintPtr(5), // 天坊冷链物流有限公司
			Unit:         "盒",
			Spec:         "300g/盒",
			Price:        25.0,
			Description:  "经典宫保鸡丁，微波加热即食",
			MinStock:     30,
			MaxStock:     150,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    90,
			Manufacturer: "方便食品有限公司",
			Remark:       "加热3分钟即可食用的成品菜",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},

		// 火锅食材
		{
			Code:         "HOTPOT-001",
			Name:         "精品火锅套餐",
			CategoryID:   categoryMap["火锅食材"],
			SupplierID:   uintPtr(5), // 天坊冷链物流有限公司
			Unit:         "盒",
			Spec:         "1.5kg/盒",
			Price:        158.0,
			Description:  "含牛肉卷、羊肉片、虾滑、豆腐等多种火锅食材",
			MinStock:     20,
			MaxStock:     100,
			Status:       1,
			StorageType:  3, // 冷冻
			ShelfLife:    120,
			Manufacturer: "海底捞食品",
			Remark:       "一站式火锅食材组合，家庭聚餐首选",
			CreatedBy:    adminID,
			UpdatedBy:    adminID,
		},
	}

	// 添加货物
	createdCount := 0
	for _, cargo := range cargos {
		if err := db.Create(&cargo).Error; err != nil {
			logger.Error("创建货物失败", zap.String("code", cargo.Code), zap.Error(err))
			return err
		}
		createdCount++
	}

	logger.Info("餐饮冻品货物数据初始化成功", zap.Int("created", createdCount))
	return nil
}
